package com.andro.inje.chansung.freeboard;

import java.util.List;

import com.andro.inje.chansung.ConstantName;
import com.andro.inje.chansung.ConstantURL;
import com.andro.inje.chansung.DB;
import com.andro.inje.chansung.R;
import com.andro.inje.chansung.noticeboard.BoardUpdateThread;
import com.andro.inje.chansung.noticeboard.Notice;
import com.andro.inje.chansung.noticeboard.NoticeAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class FreeBoardActivity extends Activity implements OnItemClickListener, OnClickListener {
	private ListView list;
	private ProgressDialog pd;
	
	private FreeBoardHandler handler;
	private Activity activity;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.free_board_layout);
		
		handler = new FreeBoardHandler();
		activity = this;
		
		list = (ListView)findViewById(R.id.free_board_list_view);
		list.setOnItemClickListener(this);
		Button reloadBtn = (Button)findViewById(R.id.free_board_reload_btn);
		reloadBtn.setOnClickListener(this);
		
		loadList();
	}
	
	public void loadList() {
		BoardUpdateThread thread = new BoardUpdateThread(ConstantURL.FREE_BOARD_URL, 1, this, handler);
		pd = ProgressDialog.show(this, "로딩중", "자유 게시판 로딩중...");
        thread.start();
	}	
	
	public class FreeBoardHandler extends Handler {
		public void handleMessage(Message msg) {
			SQLiteDatabase db = openOrCreateDatabase(ConstantName.DATABASE_NAME, MODE_WORLD_READABLE, null);
			List<Notice> notices = DB.getFreeFromFreeTable(db);
			db.close();
			
			NoticeAdapter adapter = new NoticeAdapter(activity, notices);
			
			list.setAdapter(adapter);
			pd.cancel();
			pd = null;
		}
	}

	public void onClick(View v) {
		switch( v.getId() ) {
		case R.id.free_board_reload_btn :
			loadList();
			break;
		}
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		SQLiteDatabase db = openOrCreateDatabase(ConstantName.DATABASE_NAME, MODE_WORLD_READABLE, null);
		List<Notice> notices = DB.getFreeFromFreeTable(db);
		db.close();
		
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.board_content_dialog_layout);
        dialog.setTitle(notices.get(position).getTitle());
        WebView webView = (WebView)dialog.findViewById(R.id.webView1);
        WebSettings set = webView.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);
		dialog.show();
		
		webView.loadUrl("http://203.241.246.156/chansung/InjeCSE/CSEDepartment/retrievingContent.php?page=free&wr_id=" + notices.get(position).getLink());
	}
}
